//-----------------------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------------------
matrix	LocalMtx;
matrix	WorldMtx;
matrix	ViewMtx;
matrix	ProjMtx;


bool	hasTexture;
float3 LightPos;
float3 LightDiffuse;
float3 LightAmbient;

//-----------------------------------------------------------------------------------------
// Textures
//-----------------------------------------------------------------------------------------

Texture DecalTexture;
sampler DecalSampler = sampler_state
{ 
    Texture = (DecalTexture);
	MinFilter = Linear ;
	MagFilter = Linear ;
	MipFilter = Linear ;
	AddressU = WRAP;
	AddressV = WRAP;
};

//-----------------------------------------------------------------------------------------
// VertexShader I/O
//-----------------------------------------------------------------------------------------
struct VSInput
{
    float4 Position : POSITION; 
    float2 TexCoords : TEXCOORD0;
    float3 Normal : NORMAL;
};


struct VSOutput
{
	float4 Position : POSITION; 
	float2 TexCoords : TEXCOORD0; 
	float3 Normal : TEXCOORD1; 
	float3 pos : TEXCOORD2;
};

//-----------------------------------------------------------------------------------------
// PixelShader I/O
//-----------------------------------------------------------------------------------------

struct PSInput
{
	float2 TexCoords : TEXCOORD0; 
	float3 Normal: TEXCOORD1;
	float3 pos: TEXCOORD2;
};

//-----------------------------------------------------------------------------------------
// VertexShader
//-----------------------------------------------------------------------------------------
VSOutput VS( VSInput input )
{
	VSOutput output = (VSOutput)0;
	matrix lw = mul(LocalMtx,WorldMtx);
	matrix wv = mul(lw,ViewMtx);
	matrix vp = mul(wv,ProjMtx);
	output.Position = mul(input.Position,vp);
	output.Normal = input.Normal;
	output.TexCoords = input.TexCoords;
	output.pos = mul(input.Position ,lw);

	return output;    
}
//-----------------------------------------------------------------------------------------
// PixelShader
//-----------------------------------------------------------------------------------------
float4 PS( PSInput input ) : COLOR
{
	float3 LightDir = normalize( LightPos - input.pos);
	float3 diffuse = max(dot(LightDir,input.Normal),0.0) * LightDiffuse;
	float3 decalColor = {1.0f,0.0f,0.0f};
	if(hasTexture)
	{
		decalColor = tex2D(DecalSampler, input.TexCoords).rgb;
	}
	float3 color =decalColor*diffuse + LightAmbient*decalColor;
	return float4(color.r,color.g,color.b,1.0);
}

//-----------------------------------------------------------------------------------------
// Techniques
//-----------------------------------------------------------------------------------------
technique BasicTech
{
    pass P0
    {   
		ZEnable = TRUE;
		Cullmode = CCW;
		Antialiasedlineenable = TRUE;
		VertexShader = compile vs_3_0 VS();
		PixelShader  = compile ps_3_0 PS(); 
    }
}
